package cn.djb.day18;

import java.util.LinkedList;
import java.util.Queue;

class Solution {
    /**
     * 广度优先遍历 bfs
     *
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int count = 0;
        while (!queue.isEmpty()) {
            count++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (null != poll.left) queue.add(poll.left);
                if (null != poll.right) queue.add(poll.right);
            }

        }
        return count;


    }

    /**
     * 深度优先遍历
     *
     * @param root
     * @return
     */
    public int dfs(TreeNode root) {
        if (root == null) return 0;

        return Math.max(dfs(root.left), dfs(root.right)) + 1;

    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}